package club.banyuan;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class BstTest {
  private final int BOUND = 10000;
  private final int SIZE = 5000;

  @Test
  public void bstTest() {
    Random random = new Random();
    Bst<Integer> bst = new Bst<>();
    for (int i = 0; i < SIZE; i++) bst.add(random.nextInt(BOUND));
    var list = bst.toList();
    Integer pre = Integer.MIN_VALUE;
    /*
    for (int i = 0; i < list.size(); i++) {
      Integer curr = list.get(i);
      Assertions.assertTrue(pre < curr);
      pre = curr;
    }*/
    for (Integer i : list) {
      Assertions.assertTrue(i >= pre);
      pre = i;
    }
  }

  @Test
  public void deleteTest() {
    var list = new ArrayList<Integer>();
    var bst = genBstAndList(list);
    for (Integer i : list) {
      bst.delete(i);
      verifyBst(bst);
    }
  }

  private void verifyBst(Bst<Integer> bst) {
    var list = bst.toList();
    var pre = Integer.MIN_VALUE;
    for (Integer i : list) {
      Assertions.assertTrue(i >= pre);
      pre = i;
    }
  }

  private Bst<Integer> genBstAndList(List<Integer> list) {
    var bst = new Bst<Integer>();
    var random = new Random();
    for (int i = 0; i < SIZE; i++) {
      int rand = random.nextInt(BOUND);
      bst.add(rand);
      list.add(rand);
    }
    return bst;
  }

  @Test
  void delete2Test() {
    var list = new ArrayList<Integer>();
    var bst = genBstAndList(list);

    for (Integer i : list) {
      bst.delete2(i);
      verifyBst(bst);
    }
    Assertions.assertEquals(0, bst.toList().size());
  }

  @Test
  void manualTest() {
    var bst = new Bst<Integer>();
    bst.add(4);
    bst.add(8);
    bst.add(9);
    bst.delete2(8);
    System.out.println(bst);
    bst.delete2(8);
    System.out.println(bst);
    bst.delete2(9);
    System.out.println(bst);
  }
}
